package com.shengde.jinwawaforleader.model.myJob.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMClient;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.activity.LoginActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.DemoHelper;
import com.shengde.jinwawaforleader.model.myJob.instance_message.ObserveBigPicActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProLinkmenList;
import com.shengde.jinwawaforleader.utils.DecodeImageUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * Created by Administrator on 2016/10/31 0031.
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

	Context context;
	List<ProLinkmenList.DataBean> dataBean;

	public MyExpandableListAdapter(Context context, List<ProLinkmenList.DataBean> dataBean) {
		this.context = context;
		this.dataBean = dataBean;
	}


	@Override
	public int getGroupCount() {
		return dataBean.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return dataBean.get(groupPosition).getData().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return dataBean.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return dataBean.get(groupPosition).getData().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ParentViewHolder holder = null;
		if (convertView == null) {
			holder = new ParentViewHolder();
			convertView = View.inflate(context, R.layout.instance_message_expandable_view_header, null);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.iv_more = (ImageView)convertView.findViewById(R.id.iv_more);
			holder.tv_person_num = (TextView) convertView.findViewById(R.id.tv_person_num);
			convertView.setTag(holder);
		} else {
			holder = (ParentViewHolder) convertView.getTag();
		}
		holder.tv_name.setText(dataBean.get(groupPosition).getName());
		holder.tv_person_num.setText(dataBean.get(groupPosition).getData().size() + "人");
		if(isExpanded) {
			holder.iv_more.setImageResource(R.drawable.icon_down);
		}else {
			holder.iv_more.setImageResource(R.drawable.icon_more);
		}
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null;
		if (convertView == null) {
			holder = new ChildViewHolder();
//			convertView = View.inflate(context, R.layout.every_recipe_expandable_view_content, null);
			convertView = View.inflate(context, R.layout.link_list_item, null);
			holder.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_link_telephone = (TextView) convertView.findViewById(R.id.tv_link_telephone);
			convertView.setTag(holder);
		} else {
			holder = (ChildViewHolder) convertView.getTag();
		}
		Glide.with(context).load(BaseProtocol.IMG_BASE + dataBean.get(groupPosition).getData().get(childPosition).getLogo())
				.error(R.drawable.em_default_avatar).into(holder.iv_logo);
		holder.tv_name.setText(dataBean.get(groupPosition).getData().get(childPosition).getName());
		holder.tv_link_telephone.setText(dataBean.get(groupPosition).getData().get(childPosition).getTel());
		holder.iv_logo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ObserveBigPicActivity.class);
				intent.putExtra("big_img_url", BaseProtocol.IMG_BASE + dataBean.get(groupPosition).getData().get(childPosition).getLogo());
				context.startActivity(intent);
			}
		});
		holder.tv_link_telephone.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(context, R.style.dialog);
				View contentView = LayoutInflater.from(context).inflate(
						R.layout.dialog_exit, null);
				dialog.setContentView(contentView);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
				TextView ok = (TextView) contentView.findViewById(R.id.button_ok);
				TextView textView1990 = (TextView) contentView.findViewById(R.id.textView1990);
				textView1990.setText("您确定要拨打电话么?");
				TextView cacel = (TextView) contentView.findViewById(R.id.button_cacel);
				ok.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(Intent.ACTION_DIAL);
						intent.setData(Uri.parse("tel:" + dataBean.get(groupPosition).getData().get(childPosition).getTel()));
						context.startActivity(intent);
						dialog.dismiss();
					}
				});
				cacel.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});


			}
		});
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	class ParentViewHolder {
		TextView tv_name;
		ImageView iv_more;
		TextView tv_person_num;
	}

	class ChildViewHolder {
		ImageView iv_logo;
		TextView tv_name;
		TextView tv_link_telephone;
	}
}
